package com.mrx.spring.example.controller;

import com.alibaba.fastjson2.JSON;
import com.mrx.commons.model.dto.Result;
import com.mrx.spring.example.manager.out.UserManager;
import com.mrx.spring.example.model.vo.UserVO;
import com.mrx.spring.example.service.UserService;
import com.mrx.spring.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Mr.X
 * @since 2023-10-16 14:33
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;
    @Resource
    private UserManager userManager;

    @PostMapping("/login")
    public Result<?> login(@RequestBody UserVO user) {
        logger.debug("Login: {}", user);
        Result<?> login = userManager.login(user);
        logger.info("manager login result: {}", JSON.toJSONString(login));
        return Result.success("success", userService.login(user));
    }

    @PostMapping("/login2")
    public Result<?> login2(@RequestBody UserVO user) {
        logger.debug("Login: {}", user);
        return Result.success("success", userService.login(user));
    }

    @GetMapping(value = "/whoAmi")
    public Result<?> whoAmi() {
        return Result.success(SecurityUtils.getCurrentUser());
    }

    @GetMapping(value = "/whoIsThat")
    public Result<?> whoIsThat(@RequestParam Long userId) {
        return Result.success(userService.whoIsThat(userId));
    }

}
